package com.zeng.sys.authmd.restfulmodule.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeng.nicecommon.util.MapBuilder;
import com.zeng.nicecommon.util.ResponseUtil;
import com.zeng.nicecore.validation.ValidGroup;
import com.zeng.sys.authmd.apimodule.service.UserService;
import com.zeng.sys.authmd.apimodule.vo.UserVo;
import com.zeng.sys.authmd.restfulmodule.interceptor.AuthInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    UserService userService;

    @Value("${sys.login.salt:zyl}")
    String salt;

    @RequestMapping("/list")
    public Object list(Page page, UserVo userVo) {
        List<UserVo> page1 = this.userService.findPage(page, userVo);

        ArrayList<Map<String,Object>> objects = new ArrayList<>();
        page1.forEach(e->{
            Map<String, Object> build = MapBuilder.newBuilder()
                    .put("code", e.getCode())
                    .put("updateDate", e.getUpdateDate())
                    .put("insertDate", e.getInsertDate())
                    .put("id", e.getId())
                    .put("userAccount", e.getUserAccount())
                    .put("fullName", e.getFullName())
                    .put("userConfig", e.getUserConfig())
                    .put("locked", e.getLocked())
                    .build();
            objects.add(build);

        });
        return ResponseUtil.success("操作成功")
                .addData("user", objects)
                .addData("page", page);

    }

    @RequestMapping("/detail")
        public Object detail(String code) {

        UserVo value = userService.detailUserAndRole(code);
        value.setUserPw(null);
        return ResponseUtil.success("操作成功").addData("user", value);

    }

    @RequestMapping("/add")
    public Object add(@RequestBody @Validated(ValidGroup.add.class) UserVo userVo) {


        UserVo oneByUserAccount = userService.getOneByUserAccount(userVo.getUserAccount());
        if (oneByUserAccount != null) {
            return ResponseUtil.badRequest("该用户已经存在,请更换账号");
        }
        userVo.setUserPw(userVo.getMd5Pw(salt));
        userService.addOneAndRole(userVo);
        return ResponseUtil.success("操作成功");
    }

    @RequestMapping("/update")
    public Object update(@Validated(ValidGroup.update.class) @RequestBody UserVo userVo) {
        userVo.setUserPw(userVo.getMd5Pw(salt));
        this.userService.updateUserAndRole(userVo);
        // 移除用户接口缓存
        AuthInterceptor.clearUserApiByCode(userVo.getCode());

        return ResponseUtil.success("操作成功");
    }

    @RequestMapping("/delete")
    public Object delete(String code) {
        Integer delete = this.userService.delete(code);
        return ResponseUtil.success("操作成功").addData("deleteCount", delete);
    }
    @RequestMapping("/batchDelete")
    public Object batchDelete(@RequestBody List<String> codes) {
        Integer integer = this.userService.batchDelete(codes);

        return ResponseUtil.success("操作成功").addData("deleteCount", integer);
    }
}

